<?php
import("ORG.Util.Function");//导入调试函数库
import("ORG.Util.QPage");//导入分页类
import("ORG.Util.Image");
import('ORG.Net.UploadFile');

class AdverseAction extends CommonAction{
		
	public function _initialize(){
		parent::_initialize();
		$param           =    $this->_get('_URL_');
		$controller      =    $param[0];
		$action          =    $param[1];
		//language
		$lang            =    L($action);  		
		$manage          =    L('manage');
		
		$this->assign("manage",$manage);
		$this->assign("current",$lang);
		$this->assign("lang",$lang);
	}
	
	public function adverseList(){
		$Ad        =   new Model('Ad');
		$count     =   $Ad->count();
		
		$pages       =   new QPage($count,15);
		$adlist   =   $Ad->limit($pages->limit)->select();
		$pageshow    =   $pages->fpage(4,5,6,0);
		$this->assign("adlist",$adlist);
		$this->assign("paginator",$pageshow);
		$this->display();
		
	}
	
	public function addad(){
		$this->display();
	}	
	
	public function updatead(){
		$Ad          =   new Model('Ad');
		$id          =   $this->_get('id');
				
		$datas       =   $Ad->where("ad_id=" . $id)->find();
		
		//转化时间格式
		$datas['start_time']  =   date('d/m/Y',$datas['start_time']);
		$datas['end_time']    =   date('d/m/Y',$datas['end_time']);		
		
		$this->assign("adverse",$datas);
		$this->display();
	}
	
	
	
	/*************** [ Business Logic ] ***************/
	public function addadOk(){
		$Ad         =   new Model('Ad');
		$datas      =   $Ad->create();	

		//处理时间
		$datas['start_time']    =   strtotime($datas['start_time']);
		$datas['end_time']      =   strtotime($datas['end_time']);
		$datas['createdtime']   =   time();
		
		$id         =   $Ad->add($datas);
		$url        =   U('Adverse/ad');
		
		if($id){
			$this->success("添加广告成功",$url);
		}else {
			$url    =   U('Adverse/addad');
			$this->success("添加广告失败",$url);
		}
	}
		
	/**
	 * Update Ad
	 */
	public function updateadOk(){
		$Ad        =    new Model('Ad');				
		$datas     =    $_POST;		
		$id        =    $this->_post("ad_id");	
		unset($datas['ad_id']);
		
		$result    =    $Ad->where("ad_id=".$id)->save($datas);
		$url       =    U('Adverse/ad');
		
		if($result){
			$this->success("更新成功",$url);
		}else{
			$this->success("更新失败",$url);
		}
	}
	
	/**
	 * Delete Ad
	 */
	public function deletead(){
		$Ad        =   new Model('Ad');
		$id        =   $_POST['id'];
		$con       =   "ad_id=".$id;
		
		$result    =   $Ad->where($con)->delete();
		if($result){
			$this->ajaxReturn($result,"删除成功",1);
		}else{
			$this->ajaxReturn($result,"删除失败",1);
		}
	}
	
	
	/**
	 * DeleteAd
	 * 
	 */
	public function adAjaxTable(){
		$Ad             =    new Model('Ad');
		$act                =    $_POST['action'];
		
		switch($act){
			case 'del':
				$id       =    $_POST['id'];
				$result   =    $Ad->where("ad_id=$id")->delete();
		
				if($result){
					$this->ajaxReturn($result,"删除成功",1);
				}else{
					$this->ajaxReturn($result,"删除失败",0);
				}
				break;
			default:
				//add code
				break;
		}
		
	}
	
	
	
}